package com.lagou.subjectone;

import java.util.Scanner;

public class SubjectOneMain {
    public static void main(String[] args) {
        //定义数组范围
        int[][] arrays = new int[16][16];
        //监听输入
        Scanner sc = new Scanner(System.in);
        int nextChoose = 0;
        //定义退出按键
        while (nextChoose != 5) {
            //输入用户选择项
            System.out.println("1：输入");
            System.out.println("2：打印");
            System.out.println("3：左上到右下求和");
            System.out.println("4：右上到左下求和");
            System.out.println("5：退出");
            System.out.print("请选择：");
            //监听用户选择
            nextChoose = sc.nextInt();
            //判断用户选择
            switch (nextChoose) {
                case 1:
                    //求和
                    System.out.print("输入横坐标 纵坐标 值 以空格分隔：");
                    InputValue(arrays, sc.nextInt(), sc.nextInt(), sc.nextInt());
                    break;
                case 2:
                    //打印
                    Print(arrays);
                    break;
                case 3:
                    //X轴求和
                    SumArray(arrays, true);
                    break;
                case 4:
                    //Y轴求和
                    SumArray(arrays, false);
                    break;
                case 5:
                    //退出
                    System.out.println("退出成功");
                    break;
                default:
                    //无法识别重新输入
                    System.out.println("输入错误重新输入！");
                    break;
            }
        }
    }

    /**
     * 填充数组
     *
     * @param arrays
     */
    public static void InputValue(int[][] arrays, int x, int y, int value) {
        if (x < 0 || x >= 16)
            System.out.println("输入的横坐标范围错误！");
        else if (y < 0 || y >= 16)
            System.out.println("输入的纵坐标范围错误！");
        else {
            arrays[x][y] = value;
        }

    }

    /**
     * 打印当前数组
     *
     * @param arrays
     */
    public static void Print(int[][] arrays) {
        //打印数组
        for (int i = 0; i < 16; i++) {
            for (int j = 0; j < 16; j++) {
                System.out.print(arrays[i][j] + " ");
            }
            System.out.print("\n");
        }
    }

    /**
     * 数组求和
     *
     * @param arrays
     * @param isXSum 是否是x轴求和
     */
    public static void SumArray(int[][] arrays, boolean isXSum) {
        int sum = 0;
        if (isXSum) {
            for (int i = 0; i < 16; i++) {
                sum += arrays[i][i];
            }
        } else {
            for (int i = 15; i >= 0; i--) {
                sum += arrays[i][15 - i];
            }
        }
        System.out.println((isXSum ? "X轴求和为：" : "Y轴求和为：") + sum);
    }
}
